package me.fangx.zhihu.modle.dao;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import me.fangx.zhihu.CookBookApp;
import me.fangx.zhihu.modle.bean.CookBean;
import me.fangx.zhihu.utils.BaseUtil;

/**
 * 作者： Owen
 * 时间： 2016/6/28 17:35
 * 类说明：
 */
public class DBManager {

//    public static final String DATABASE_PATH = android.os.Environment
//            .getExternalStorageDirectory().getAbsolutePath() + "/cookbook";
    public static final String DATABASE_PATH = CookBookApp.getContext().getFilesDir().getAbsolutePath();
    public static final String DATABASE_FILENAME = "cookbook.db";

    public static void copyDatabase(){
        try {
            String databaseFilename = DATABASE_PATH + "/" + DATABASE_FILENAME;
            File dir = new File(DATABASE_PATH);
            if (!dir.exists())
                dir.mkdir();
            if (!(new File(databaseFilename)).exists()) {
                InputStream is = CookBookApp.getContext().getResources().getAssets().open("cookbook.db");
                FileOutputStream fos = new FileOutputStream(databaseFilename);
                byte[] buffer = new byte[1024];
                int count = 0;
                while ((count = is.read(buffer)) > 0) {
                    fos.write(buffer, 0, count);
                }
                fos.close();
                is.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 初始化数据库
    public SQLiteDatabase openDatabase() {
        String databaseFilename = DATABASE_PATH + "/" + DATABASE_FILENAME;
        SQLiteDatabase database = null;
        try {
            database = SQLiteDatabase.openOrCreateDatabase(databaseFilename, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return database;
    }

    public DBManager(){
    }

    public List<CookBean> getCookList(int type, int offset, int limit){
        List<CookBean> list = new ArrayList<>();
//        String sql = "select * from cooks where type="+type+" order by _id limit "+offset+","+limit+";";
        SQLiteDatabase database = openDatabase();
        if (database!=null&&database.isOpen()) {
            Cursor cursor = database.rawQuery("select * from "+getFormName()+" where type=? order by _id limit ?,?", new String[]{type+"", offset+"", limit+""});
            while(cursor.moveToNext()){
                int id = cursor.getInt(cursor.getColumnIndex("_id"));
                int pictureId = cursor.getInt(cursor.getColumnIndex("pic_id"));
                int cooktype = cursor.getInt(cursor.getColumnIndex("type"));
                int isCollect = cursor.getInt(cursor.getColumnIndex("is_favorite"));
                String name = cursor.getString(cursor.getColumnIndex("name"));
                String material = cursor.getString(cursor.getColumnIndex("material"));
                String practice = cursor.getString(cursor.getColumnIndex("practice"));
                list.add(new CookBean(id, pictureId, cooktype, isCollect, name, material, practice));
            }
            database.close();
        }
        return list;
    }

    public List<CookBean> getCollectList(int offset, int limit){
        List<CookBean> list = new ArrayList<>();
//        String sql = "select * from cooks where is_favorite=1 order by _id limit "+offset+","+limit+";";
        SQLiteDatabase database = openDatabase();
        if (database!=null&&database.isOpen()) {
            Cursor cursor = database.rawQuery("select * from "+getFormName()+" where is_favorite=1 order by _id limit ?,?", new String[]{offset+"", limit+""});
            while(cursor.moveToNext()){
                int id = cursor.getInt(cursor.getColumnIndex("_id"));
                int pictureId = cursor.getInt(cursor.getColumnIndex("pic_id"));
                int cooktype = cursor.getInt(cursor.getColumnIndex("type"));
                int isCollect = cursor.getInt(cursor.getColumnIndex("is_favorite"));
                String name = cursor.getString(cursor.getColumnIndex("name"));
                String material = cursor.getString(cursor.getColumnIndex("material"));
                String practice = cursor.getString(cursor.getColumnIndex("practice"));
                list.add(new CookBean(id, pictureId, cooktype, isCollect, name, material, practice));
            }
            database.close();
        }
        return list;
    }

    public void collectCook(int id, int isCollect){
//        String sql = "update cooks set is_favorite="+isCollect+" where _id="+id;
        SQLiteDatabase database = openDatabase();
        if (database!=null&&database.isOpen()) {
            database.execSQL("update recipe_th set is_favorite=? where _id=?", new Object[]{isCollect, id});
            database.execSQL("update recipe_en set is_favorite=? where _id=?", new Object[]{isCollect, id});
            database.close();
        }
    }

    public static String getFormName(){
        String langugue = BaseUtil.getCountryLangue();
        String formName = "";
        if("th".equals(langugue)||"tr".equals(langugue)){
            formName = "recipe_th";
        }else{
            formName = "recipe_en";
        }
        return formName;
    }

}
